#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=1e6+10;
int prime[N];
int v[N];
int m=0;
int a[N];
void get_prime(int n)
{
	
	for (int i = 2; i<=n; i++) {
	if (v[i] == 0) { 
		v[i] = i;
		prime[++m] = i;
	}
	for (int j = 1; j <= m; j++) {
		if (prime[j] > v[i] || prime[j] > n/i) break;
		v[i*prime[j]] = prime[j];
	}
}

}
int check(int n)
{
	int sum=0;
	while(n)
	{
		sum=sum+n%10;
		n/=10;
	 }
	 if(sum==v[sum]) return 1;
	 return 0;
}
int main()
{
	get_prime(1000000);
	for(int i=1;i<=m;i++)
	{
		if(check(prime[i]))
		a[prime[i]]=1;
	}
	for(int i=1;i<=1000000;i++)
	{
		a[i]=a[i]+a[i-1];
	}
	int t;
	cin>>t;
	int i=0;
	while(t--)
	{
		i++;
		int l,r;
		cin>>l>>r;
		printf("Case #%d: %d\n",i,a[r]-a[l-1]);
	}

	return 0;
}

